package panda.natalia.gothread.suspend.instrument;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.TypeInsnNode;
import org.objectweb.asm.tree.analysis.BasicValue;

/**
 * a simple framework of java fiber
 *
 * @Author: scpanda2007
 * @Date: 2019/11/28 16:57
 *
 */
class NewValue extends BasicValue {

    public final boolean isDupped;
    public final AbstractInsnNode insnNode;


    public NewValue(Type type, boolean isDupped, AbstractInsnNode insnNode) {
        super(type);
        this.isDupped = isDupped;
        this.insnNode = insnNode;
    }


    /**
     * eg.
     *
     * TestCode code1 = new TestCode();
     *
     * LINENUMBER 15 L0
     *     NEW panda/natalia/gothread/suspend/instrument/TestCode
     *     DUP
     *     INVOKESPECIAL panda/natalia/gothread/suspend/instrument/TestCode.<init> ()V
     *     ASTORE 1
     *
     *  注意 TestCode.<init> ()V 说明 这个函数是不会返回创建的实例的, 而它会消耗一个参数（即前面new时得到的变量指针）
     *   所以这个地方 会有一次 dup 操作
     *
     * @return
     */
    String formatInsn() {
        switch (insnNode.getOpcode()){
            case Opcodes.NEW:
                return "NEW " + ((TypeInsnNode) insnNode).desc;
            case Opcodes.DUP:
                return "DUP";
            default:
                return "UNEXPECTED INSTRUCTION " + insnNode.getOpcode();
        }
    }
}
